package com.neulogistics.core.customercenter.api.application.converter;

import cn.hutool.json.JSONUtil;

import com.neulogistics.core.customercenter.api.application.viewmodel.OrderViewModel;
import com.neulogistics.core.customercenter.domain.model.valueobject.Address;
import com.neulogistics.core.customercenter.domain.model.valueobject.OrderItem;
import com.neulogistics.core.customercenter.infrastructure.persistence.orm.po.OrderPO;
import org.springframework.beans.BeanUtils;

public class OrderPOVMConverter {
    private static final int Rate = 100;

    public static OrderViewModel POToVM(OrderPO orderPO){
        if (orderPO == null)
            return null;

        OrderViewModel orderViewModel = new OrderViewModel();
        BeanUtils.copyProperties(orderPO, orderViewModel);
        // long转string
        orderViewModel.setOrderId(orderPO.getOrderId().toString());
        // json对象转换为值对象
        orderViewModel.setReceiverAddress(JSONUtil.toBean(orderPO.getReceiverAddress(), Address.class));
        orderViewModel.setOrderItems(JSONUtil.toList(orderPO.getOrderItems(), OrderItem.class));
        // 价格转换
        orderViewModel.setTotalAmount(Double.valueOf(orderPO.getTotalAmount()) / Rate);

        return orderViewModel;
    }
}
